<?php
/**
 * Miffos framework
 * ----------------------------------------------
 *
 * Copyright (c) 2008, 2009 Martin Sadový (http://sodae.cz)
 *
 * This source file is subject to the "Miffos license" that is bundled
 * with this package in the file license.txt.
 *
 * @copyright  Copyright (c) 2008, 2009 Martin Sadový
 * @license    license.txt
 * @link       http://miffos.sodae.cz
 * @package    miffos
 * @version    $Id$
 */

require_once dirname(__FILE__).'/SessionHandler.php';

class Session {

    /**
    * If started session
    * @var static bool
    */
    private static $started = false;

    /**
    * Session Instance
    * @var static array
    */
    private static $namespaceInstances = array();

    /**
    * Default name namespace
    * @var string
    */
    private $namespace = "default";

    /**
    * Name Handler for Session
    * @var string
    */
    private $namespaceHandler = "NamespaceSessionHandler";



    public function __construct($namespace = null)
    {
        if(!is_null($namespace)) $this->namespace = $namespace;
        if(defined('SID')){
           self::$started = true;
        }else{
           $this->start();
        }
    }



    /**
    * Start session
    * @return void
    */
    public function start()
    {
        if(self::$started)
            throw new Exception("Session already started");
        session_start();
        self::$started = true;
    }



    /**
    * Get session id
    * @return string
    */
    public function getId()
    {
        return session_id();
    }




    /**
    * Set session Handler
    * @param string $void
    */
    public function setNamespaceHandler($name)
    {
        if(!is_string($name))
           throw new Exception("Namespace Handler must be string !");
        if(!class_exists($name,false) )
           throw new Exception("Session Handler '".$name."' doesn't exist !");

        $this->namespaceHandler = $name;
    }



    /**
    * Read Session Namespace
    * @param string $name
    * @return mixed
    */
    public function getNamespace($namespace)
    {
        if(!self::$started){
           $this->start();
        }

        if(!isset(self::$namespaceInstances[$namespace])){
           $namespaceHandler = $this->namespaceHandler;
           self::$namespaceInstances[$namespace] = new $namespaceHandler($_SESSION['_NS'][$namespace]);
        }

        return self::$namespaceInstances[$namespace];
    }



    /**
    * Regenerate Id
    * @return void
    */
    public function regenerateId()
    {
        if(!self::$started)
           throw new Exception("Session doesn't started.");
        session_regenerate_id(true);
    }



    /**
    * Destroy session data
    * @return void
    */
    public function destroy()
    {
        if(!self::$started)
           throw new Exception("Session doesn't started.");
        $_SESSION = null;
        session_destroy();
    }



    /**
    * Set Expire for namespace
    * @param int $second
    */
    public function setExpiration($seconds)
    {
        session_set_cookie_params($seconds);
        $this->regenerateId();
        setcookie( session_name(), $this->getId(), $seconds + time() , '/' );
    }


    /**
    * Magic method
    * @return object
    */
    public function __get($name)
    {
        return $this->getNamespace($this->namespace)->$name;
    }

    public function __set($name,$value)
    {
        $this->getNamespace($this->namespace)->$name = $value;
    }

    public function __unset($name)
    {
        unset($this->getNamespace($this->namespace)->$name);
    }

    public function __isset($name)
    {
        return isset($this->getNamespace($this->namespace)->$name);
    }

}

?>